<?php
/**
 * 2012.8.24
 * @author uther,tjb
 *
 */
class m_rebate extends CI_Model {

	var $table = 'rebate';
	var $t2 = 'tbk_report';
	var $pri_name = 'rid';
	var $perpage = 10;
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
	private function _search()
	{
		$keywords = $this->input->post('keywords', TRUE);
		if ($keywords){
			foreach($this->like_field AS $key => $field){
				if($key)$this->db->like($field, $keywords);
				else $this->db->or_like($field, $keywords);
			}
		}
	}
	
	private function _set()
	{
		$this->pageno = $this->input->post('pageNum', TRUE);
		if ($this->pageno < 1) $this->pageno = 1;
		
		$numPerPage = $this->input->post('numPerPage', TRUE);
		$this->perpage = $numPerPage ? $numPerPage : $this->perpage;
		
		
		
		$order = $this->input->post('orderField', TRUE);
		$order = $order ? $order : $this->pri_name;
		
		$sort_by = $this->input->post('orderDirection', TRUE);
		$sort_by = $sort_by ? $sort_by : 'ASC';
		$this->db->order_by($order, $sort_by);
	}
	
    public function _list(){
    	$this->_search();
    	$this->_set();
    	$this->db->select('*');
    	$where = array('can_rebate'=>1,'is_rebate'=>0);
		$this->db->where($where);
		$query = $this->db->get($this->table);
		
		return $query->result_array();
    }
    
    public function tot_list($data = array()){
    	$this->db->select('*');
    	$this->db->limit($data['limit'],$data['page']);
    	$res = $this->db->get($this->table);
    	return $res->result_array();
    }
    
	public function check_can(){
    	$sql = "UPDATE `tb_tbk_report` as t LEFT JOIN `tb_rebate` as r ON t.trade_parent_id=r.trade_id SET t.is_fanli=1,t.fanli_money=t.commission/2,t.fanli_time=".time().",r.can_rebate=1,r.rebate_cash=t.commission/2 WHERE r.trade_id>0 AND r.can_rebate=0 AND t.is_fanli=0 ";
    	$res = $this->db->query($sql);
    	return $this->_list();
    }
    
    public function user_can(){
    	$sql = "SELECT r.rid,(r.can_rebate+1) as can_rebate FROM `tb_rebate` as r LEFT JOIN `tb_user` as u ON r.uid=u.id WHERE r.uid>0 AND r.can_rebate=0 AND r.rebate_cash<(u.fanli-u.is_fanli) ";
    	$r = $this->db->query($sql);
    	if($r->num_rows){
    		$data = $r->result_array();
    		$this->db->update_batch('rebate', $data, 'rid');
    	}
    	return $this->_list();
    }
    
	public function _done($id=''){
		$sql = "SELECT `uid` FROM `tb_rebate` WHERE rid=$id";
		$res = $this->db->query($sql);
		$uid = $res->result_array();
		if($uid){
			$sql = "UPDATE `tb_user` as u LEFT JOIN `tb_rebate` as r ON r.uid=u.id SET u.is_fanli=u.is_fanli+r.rebate_cash,r.is_rebate=1,r.rebate_date=".time()." WHERE r.is_rebate=0 AND u.id>0";
			$this->db->query($sql);
			$res = TRUE;
		}
		else{
			$this->db->where('rid',$id);
			$data = array('is_rebate'=>1,'rebate_date'=>time());
			$this->db->set($data);
			$res = $this->db->update($this->table);
		}

		return $res;
	}
	
	public function _tot(){
		
		$sql = "SELECT r.outer_code AS id, (sum( r.commission /2 )+u.fanli) as fanli FROM `tb_tbk_report` AS r LEFT JOIN `tb_user` AS u ON u.id=r.outer_code WHERE r.is_fanli = 0 AND r.outer_code >0 GROUP BY r.outer_code";
		$r = $this->db->query($sql);
		if($r->num_rows()){
			$data = $r->result_array();
			$sql = "UPDATE `tb_tbk_report` SET is_fanli=1,fanli_money=commission/2,fanli_time=".time()." WHERE is_fanli=0 AND outer_code>0";
			$this->db->query($sql);
			$this->db->update_batch('user', $data, 'id');
			return $data;
		}
		else{
			return false;
		}
	}
}